
// 蓝牙设备id，可通过onBluetoothDeviceFound方法获取
var deviceId = '';
// 蓝牙服务id，可通过getBLEDeviceServices方法获取
var serviceId = '';
// 蓝牙特征值id，可通过getBLEDeviceCharacteristics方法获取
var characteristicId = '';

// 打开蓝牙模块
function openBluetoothAdapter(){
	plus.bluetooth.openBluetoothAdapter({
		success:function(e){
			console.log('open success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('open failed: '+JSON.stringify(e));
		}
	});
}

// 获取蓝牙状态
function getBluetoothState(){
	plus.bluetooth.getBluetoothAdapterState({
		success:function(e){
			console.log('state success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('state failed: '+JSON.stringify(e));
		}
	});
}

// 开始搜索蓝牙
function startBluetoothDiscovery(){
	plus.bluetooth.openBluetoothAdapter({
		success:function(e){
			console.log('open success: '+JSON.stringify(e));
			plus.bluetooth.startBluetoothDevicesDiscovery({
				success:function(e){
					console.log('start discovery success: '+JSON.stringify(e));
				},
				fail:function(e){
					console.log('start discovery failed: '+JSON.stringify(e));
				}
			});
		},
		fail:function(e){
			console.log('open failed: '+JSON.stringify(e));
		}
	});
}
// 获取已连接的蓝牙设备
function getConnectedDevices(){
	plus.bluetooth.getConnectedBluetoothDevices({
		success:function(e){
			var devices = e.devices;
			console.log('connected devices success: '+e.length);
			for(var i in devices){
				console.log(i+': '+JSON.stringify(devices[i]));
			}
		},
		fail:function(e){
			console.log('connected devices failed: '+JSON.stringify(e));
		}
	});
}
// 获取已搜索到的蓝牙设备
function getBluetoothDevices(){
	plus.bluetooth.getBluetoothDevices({
		success:function(e){
			var devices = e.devices;
			console.log('get devices success: '+e.length);
			for(var i in devices){
				console.log(i+': '+JSON.stringify(devices[i]));
			}
		},
		fail:function(e){
			console.log('get devices failed: '+JSON.stringify(e));
		}
	});
}

// 监听状态变化
function listenerStateChange(){
	plus.bluetooth.onBluetoothAdapterStateChange(function(e){
		console.log('state changed: '+JSON.stringify(e));
	});
}
// 连接蓝牙设备

function createConnection(){
	plus.bluetooth.createBLEConnection({
		deviceId:deviceId,
		success:function(e){
			console.log('create connection success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('create connection failed: '+JSON.stringify(e));
		}
	});
}
// 断开蓝牙设备
function closeConnection(){
	plus.bluetooth.closeBLEConnection({
		deviceId:deviceId,
		success:function(e){
			console.log('close success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('close failed: '+JSON.stringify(e));
		}
	});
}
// 监听发现新设备
function listenerDeviceFound(){
	plus.bluetooth.onBluetoothDeviceFound(function(e){
		var devices = e.devices;
		console.log('device found: '+e.length);
		for(var i in devices){
			console.log(i+': '+JSON.stringify(devices[i]));
		}
	});
}
// 获取蓝牙设备指定服务中所有特征值
function getCharacteristics(){
	plus.bluetooth.getBLEDeviceCharacteristics({
		deviceId:deviceId,
		serviceId:serviceId,
		success:function(e){
			var characteristics = e.characteristics;
			console.log('get characteristics success: '+characteristics.length);
			for(var i in characteristics){
				console.log(i+': '+JSON.stringify(characteristics[i]));
			}
		},
		fail:function(e){
			console.log('get characteristics failed: '+JSON.stringify(e));
		}
	});
}

// 获取蓝牙设备的所有服务
function getServices(){
	plus.bluetooth.getBLEDeviceServices({
		deviceId:deviceId,
		success:function(e){
			var services = e.services;
			console.log('get services success: '+services.length);
			for(var i in services){
				console.log(i+': '+JSON.stringify(services[i]));
			}
		},
		fail:function(e){
			console.log('get services failed: '+JSON.stringify(e));
		}
	});
}

// 启用低功耗蓝牙设备特征值变化时的notify功能
function startCharacteristicsNotify(){
	// 监听低功耗蓝牙设备的特征值变化 
	plus.bluetooth.onBLECharacteristicValueChange(function(e){
		console.log('onBLECharacteristicValueChange: '+JSON.stringify(e));
		var value = buffer2hex(e.value);
		console.log('value(hex) = '+value);
		if(characteristicId == e.characteristicId){
			// 更新到页面显示
			alert('特征值变化: '+value);
		}
	});
	// 启用notify功能
	plus.bluetooth.notifyBLECharacteristicValueChange({
		deviceId:deviceId,
		serviceId:serviceId,
		characteristicId:characteristicId,
		success:function(e){
			var characteristics = e.characteristics;
			console.log('get characteristics success: '+characteristics.length);
			for(var i in characteristics){
				console.log(i+': '+JSON.stringify(characteristics[i]));
			}
		},
		fail:function(e){
			console.log('get characteristics failed: '+JSON.stringify(e));
		}
	});
}


// 启用低功耗蓝牙设备特征值变化时的notify功能
function startCharacteristicsNotify(){
	// 监听低功耗蓝牙设备的特征值变化 
	plus.bluetooth.onBLECharacteristicValueChange(function(e){
		console.log('onBLECharacteristicValueChange: '+JSON.stringify(e));
		var value = buffer2hex(e.value);
		console.log('value(hex) = '+value);
		if(characteristicId == e.characteristicId){
			// 更新到页面显示
			alert('特征值变化: '+value);
		}
	});
	// 启用notify功能
	plus.bluetooth.notifyBLECharacteristicValueChange({
		deviceId:deviceId,
		serviceId:serviceId,
		characteristicId:characteristicId,
		success:function(e){
			var characteristics = e.characteristics;
			console.log('get characteristics success: '+characteristics.length);
			for(var i in characteristics){
				console.log(i+': '+JSON.stringify(characteristics[i]));
			}
		},
		fail:function(e){
			console.log('get characteristics failed: '+JSON.stringify(e));
		}
	});
}
// 写入低功耗蓝牙设备的特征值
function writeCharacteristics(){
	plus.bluetooth.writeBLECharacteristicValue({
		deviceId:deviceId,
		serviceId:serviceId,
		characteristicId:characteristicId,
		value:value,
		success:function(e){
			console.log('write characteristics success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('write characteristics failed: '+JSON.stringify(e));
		}
	});
}

// 读取低功耗蓝牙设备的特征值
function readCharacteristics(){
	plus.bluetooth.readBLECharacteristicValue({
		deviceId:deviceId,
		serviceId:serviceId,
		characteristicId:characteristicId,
		success:function(e){
			console.log('read characteristics success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('read characteristics failed: '+JSON.stringify(e));
		}
	});
}

// 结束搜索蓝牙
function stopBluetoothDiscovery(){
	plus.bluetooth.stopBluetoothDevicesDiscovery({
		success:function(e){
			console.log('stop discovery success: '+JSON.stringify(e));
			plus.bluetooth.closeBluetoothAdapter({
				success:function(e){
					console.log('close success: '+JSON.stringify(e));
				},
				fail:function(e){
					console.log('close failed: '+JSON.stringify(e));
				}
			});
		},
		fail:function(e){
			console.log('stop discovery failed: '+JSON.stringify(e));
		}
	});
}

// 关闭蓝牙模块
function closeBluetoothAdapter(){
	plus.bluetooth.closeBluetoothAdapter({
		success:function(e){
			console.log('close success: '+JSON.stringify(e));
		},
		fail:function(e){
			console.log('close failed: '+JSON.stringify(e));
		}
	});
}